package com.webex.ta.hydra.gui.wm;

import com.webex.ta.hydra.gui.util.PlainIconButton;
import com.webex.ta.hydra.util.Comparing;
import com.webex.ta.hydra.util.HydraUtils;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-10-20
 * Time: 14:09:53
 */
public class InternalDecorator extends JPanel {

    private static final Color ACTIVE_HEADER_COLOR = new Color(160, 186, 213);
    private static final Color INACTIVE_HEADER_COLOR = new Color(128, 128, 128);

    private static final Icon SLIDING_ICON = HydraUtils.getImage("wm/sliding.png");
    private static final Icon SLIDING_ICON_INACTIVE = HydraUtils.getImage("wm/slidingInactive.png");
    private static final Icon DOCKED_ICON = HydraUtils.getImage("wm/docked.png");
    private static final Icon DOCKED_ICON_INACTIVE = HydraUtils.getImage("wm/dockedInactive.png");

    private static final Icon AUTOHIDE_ON_ICON = HydraUtils.getImage("wm/autohideOn.png");
    private static final Icon AUTOHIDE_ON_ICON_INACTIVE = HydraUtils.getImage("wm/autohideOnInactive.png");
    private static final Icon AUTHOHIDE_OFF_ICON = HydraUtils.getImage("wm/autohideOff.png");
    private static final Icon AUTHOHIDE_OFF_ICON_INACTIVE = HydraUtils.getImage("wm/autohideOffInactive.png");

    private static final Icon HIDE_ICON = HydraUtils.getImage("wm/hideToolWindow.png");
    private static final Icon HIDE_ICON_INACTIVE = HydraUtils.getImage("wm/hideToolWindowInactive.png");

    private static final Icon FLOATING_ICON = HydraUtils.getImage("wm/floating.png");
    private static final Icon FLOATING_ICON_INACTIVE = HydraUtils.getImage("wm/floatingInactive.png");
    private static final Icon FIX_ICON = HydraUtils.getImage("wm/fix.png");
    private static final Icon FIX_ICON_INACTIVE = HydraUtils.getImage("wm/fixInactive.png");

    private TitlePanel titlePanel;
    private PlainIconButton toggleFloatingButton;
    private Separator sep1;
    private PlainIconButton toggleDockButton;
    private Separator sep2;
    private PlainIconButton toggleAutoHideButton;
    private Separator sep3;
    private PlainIconButton hideButton;

    private JComponent content;

    private WindowInfo info;

    public InternalDecorator(JComponent content, WindowInfo info) {
        super();
        this.enableEvents(ComponentEvent.COMPONENT_EVENT_MASK);
        setLayout(new BorderLayout());
        this.content = content;
        this.info = info;
//        this.addMouseListener(new MouseAdapter() {
//            @Override
//            public void mousePressed(MouseEvent e) {
//                setActive(true);
//                InternalDecorator.this.requestFocus();
//            }
//        });
        titlePanel = new TitlePanel(this);
        sep1 = new Separator();
        sep2 = new Separator();
        sep3 = new Separator();
        toggleFloatingButton = new PlainIconButton(FIX_ICON_INACTIVE);
        toggleFloatingButton.addActionListener(new ToggleFloatingAction());
        toggleDockButton = new PlainIconButton(DOCKED_ICON_INACTIVE);
        toggleAutoHideButton = new PlainIconButton(AUTHOHIDE_OFF_ICON_INACTIVE);
        hideButton = new PlainIconButton(HIDE_ICON_INACTIVE);
        hideButton.addActionListener(new HideAction());

        JPanel buttons = new JPanel(new GridBagLayout());
        buttons.setOpaque(false);
        buttons.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 2));
        buttons.add(toggleFloatingButton,
                new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
        buttons.add(sep1,
                new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
//        buttons.add(toggleDockButton,
//                new GridBagConstraints(2, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
//        buttons.add(sep2,
//                new GridBagConstraints(3, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
        buttons.add(toggleAutoHideButton,
                new GridBagConstraints(4, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
        buttons.add(sep3,
                new GridBagConstraints(5, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
        buttons.add(hideButton,
                new GridBagConstraints(6, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

        JLabel title = new JLabel(info.getTitle());
        title.setFont(title.getFont().deriveFont(Font.BOLD));
        title.setForeground(Color.white);
        titlePanel.addTitle(title);
        titlePanel.addButtons(buttons);

        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.add(titlePanel, BorderLayout.NORTH);
        contentPane.add(content, BorderLayout.CENTER);
//        contentPane.setBorder(new InnerPanelBorder());
        contentPane.setBorder(BorderFactory.createLineBorder(Color.gray));

        add(contentPane, BorderLayout.CENTER);
    }

    protected void processComponentEvent(ComponentEvent e) {
        super.processComponentEvent(e);
        if (ComponentEvent.COMPONENT_RESIZED == e.getID()) {
            WindowInfo info = this.getWindowInfo();
            if (info.isFloating()) {
                final Window owner = SwingUtilities.getWindowAncestor(this);
                if (owner != null) {
                    info.setFloatingBounds(owner.getBounds());
                }
            } else {
                ToolWindowPane twp = ToolWindowManager.getInstance().getToolWindowPane();
                if (info.getAnchor() == SwingConstants.TOP || info.getAnchor() == SwingConstants.BOTTOM) {
                    info.setWeight((float) this.getHeight() / (float) twp.getHeight());
                } else {
                    info.setWeight((float) this.getWidth() / (float) twp.getWidth());
                }
            }
        }
    }

    public void apply(final WindowInfo info) {
        if (Comparing.equal(this.info, info))
            return;

        this.info = info;
        final boolean active = info.isActive();
        titlePanel.setActive(active);

        sep1.setActive(active);
        sep2.setActive(active);
        sep3.setActive(active);

        toggleFloatingButton.setIcon(info.isFloating() ?
                (active ? FIX_ICON : FIX_ICON_INACTIVE) :
                (active ? FLOATING_ICON : FLOATING_ICON_INACTIVE));
        toggleDockButton.setIcon(active ? DOCKED_ICON : DOCKED_ICON_INACTIVE);
        toggleAutoHideButton.setIcon(active ? AUTHOHIDE_OFF_ICON : AUTHOHIDE_OFF_ICON_INACTIVE);
        hideButton.setIcon(active ? HIDE_ICON : HIDE_ICON_INACTIVE);

        validate();
        repaint();
    }

    public void setActive(boolean active) {
        WindowInfo info = getWindowInfo().copy();
        info.setActive(active);
        apply(info);
    }


    public WindowInfo getWindowInfo() {
        return info;
    }


    private static final class Separator extends JLabel {
        private static final Icon activeSeparator = HydraUtils.getImage("wm/separator.png");
        private static final Icon inactiveSeparator = HydraUtils.getImage("wm/inactiveSeparator.png");

        public Separator() {
            setActive(false);
        }

        public final void setActive(final boolean active) {
            setIcon(active ? activeSeparator : inactiveSeparator);
        }

        public final void updateUI() {
            super.updateUI();
            setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
        }
    }

    private class InnerPanelBorder implements Border {
        public boolean isBorderOpaque() {
            return true;
        }

        public Insets getBorderInsets(final Component c) {
            Insets insets = new Insets(0, 0, 0, 0);
            if (InternalDecorator.this.info != null) {
                if (InternalDecorator.this.info.getAnchor() == SwingConstants.TOP) {
//                    insets.top =
                }
            }
            return insets;
        }

        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            g.setColor(Color.red);

//            Insets insets = getBorderInsets(c);

//            if (insets.left > 0)
            g.drawLine(x, y, x, y + height - 1);


//            if (insets.right > 0)
            g.drawLine(x + width - 1, y, x + width - 1, y + height - 1);


//            if (insets.bottom > 0)
            g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);

        }
    }


    private class HideAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            ToolWindowManager.getInstance().hideToolWindow(InternalDecorator.this.getWindowInfo().getId());
        }
    }

    private class ToggleFloatingAction implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            ToolWindowManager.getInstance().toggleFloatingToolWindow(
                    InternalDecorator.this.getWindowInfo().getId()
            );
        }
    }
}
